import { useChatStore } from '@/stores'
import { ref } from 'vue'

export function useChat() {
  const chatStore = useChatStore()

  const getChatByUuidAndIndex = (uuid: number, index: number) => {
    return chatStore.getChatByUuidAndIndex(uuid, index)
  }

  const addChat = (uuid: number, chat: Chat.Chat) => {
    chatStore.addChatByUuid(uuid, chat)
  }

  const updateChat = (uuid: number, index: number, chat: Chat.Chat) => {
    chatStore.updateChatByUuid(uuid, index, chat)
  }

  const updateChatSome = (uuid: number, index: number, chat: Partial<Chat.Chat>) => {
    chatStore.updateChatSomeByUuid(uuid, index, chat)
  }

  const chatStatusType = {
    INIT: 'event: session_init',
    MESSAGE: 'event: message',
    COMPLETE: 'event: complete',
    ERROR: 'event: error',
    PATH: 'event: path',
  }


  const isChatStatusEvent = (event: string) => {
    return Object.values(chatStatusType).includes(event)
  }

  const chatStatus = ref<string>(chatStatusType.INIT)
  return {
    addChat,
    updateChat,
    updateChatSome,
    getChatByUuidAndIndex,
    chatStatus,
    isChatStatusEvent,
    chatStatusType
  }
}

export const removeThinkTags = (input: string) => {
  return input.replace(/<think>(.*?)<\/think>/, "").replace(/^<think>/, "");
}
